/**
 * Created by DevilsEyes on 2016/6/10.
 */
'use strict';
let {P, getConfigs} = require('./Point');
let {Line} = require('./Line');
let scale = 0.6;

class Zodiac {
    constructor(map) {
        let {vectors, range, lines} = map;
        let {focalLength, focus}= getConfigs();
        range = range * scale;

        this.nodes = vectors.map((p)=> {
            let x = Math.random() * range * 2 - range;
            let y = (x - focus.x) * p[0] / focalLength;
            let z = (x - focus.x) * p[1] / focalLength;
            return P(x, y, z);
        });
        this.anwser = P(100, 0, 0);

        this.lines = lines.map((l)=>new Line(this.nodes[l[0]], this.nodes[l[1]]));
        this.rX = 0;
        this.rY = 0;
        let tX = Math.random() * Math.PI * 2 - Math.PI;
        let tY = Math.random() * Math.PI * 2 - Math.PI;
        this.turnX(tX);
        this.turnY(tY);
    }

    turnX(alpha) {
        this.nodes.forEach((p)=>p.turnX(alpha));
        this.anwser.turnX(alpha);
        this.rX += alpha;
        if (Math.abs(this.rX) > Math.PI)this.rX > 0 ? this.rX -= Math.PI * 2 : this.rX += Math.PI * 2;
        this.fix();
    }

    turnY(alpha) {
        this.nodes.forEach((p)=>p.turnY(alpha));
        this.anwser.turnY(alpha);
        this.rY += alpha;
        if (Math.abs(this.rY) > Math.PI)this.rY > 0 ? this.rY -= Math.PI * 2 : this.rY += Math.PI * 2;
        this.fix();
    }

    fix() {
        this.nodes.forEach((p)=>p.save());
        this.anwser.save();
    }

    get precent() {
        return 1 - Math.acos(this.anwser.x / 100) / Math.PI / 2;
    }

    ResetX(cb) {
        let rX, p = this.anwser;
        rX = Math.atan(p.y / p.x);
        if (p.y < 0)rX += Math.PI;
        cb(-rX);
    }

    ResetY(cb) {
        let rY, p = this.anwser;
        rY = Math.atan(p.z / p.x);
        if (p.x < 0)rY += Math.PI;
        cb(-rY);
    }

}
module.exports = {
    Zodiac
};